/**
 * Created by qianjy on 2016/11/29.
 */
require.config(window.jshead);
require(['jquery', 'navigator'], function ($) {
    $.ajax({
        type: "POST",
        url: "/user/security/information",
        dataType: "json",
        success: function (data) {
            console.log(data)
            if (data.security.realnameStatus == 0) {
                layer.alert('未进行实名认证，即将跳转至实名认证页面');
                function jump() {
                    window.location.href ='/user/security/realname'
                }
                window.setTimeout(jump, 1500);
            }

            //银行卡存在的时候样式
            if(data.bank!=null){
                $('.bankCard').attr('type', data.bank.bankCode);
                $('.bankCardName').text(data.bank.bankName);
                $('.bankCard-num').text(data.bank.cardNo);
            }else {
                layer.alert('未进行绑卡，即将跳转至绑卡页面');
                function jump() {
                    window.location.href ='/user/security/realname'
                }
                window.setTimeout(jump, 1500);
            }

        }
    });
    $('#addcard').bind('click',function(){
        window.location.href='/user/security/realname';
    });
    $('#finishcard').bind('click',function(){
        $.ajax({
            type: "POST",
            url: "/account/addXfBank",
            dataType: "text",
            success: function (data) {
                $('.rebox').hide();
                location.reload();
            },error:function(){

            }
        });
    });

    $('#popupclose').bind('click',function(){
        $('.rebox').hide();
    });


    //三位数字加一个逗号并且保留两位小数
    function threeNumber(num) {
        num = num.toString().replace(/\$|\,/g,'');
        if(isNaN(num))
            num = "0";
        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num*100+0.50000000001);
        cents = num%100;
        num = Math.floor(num/100).toString();
        if(cents<10)
            cents = "0" + cents;
        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
            num = num.substring(0,num.length-(4*i+3))+','+
                num.substring(num.length-(4*i+3));
        return (((sign)?'':'-') + num + '.' + cents);
    }
    //查询银行卡充值金额
    $.ajax({
        type: 'post',
        url: '/account/recharge/rechargeLimit',
        dataTape: 'json',
        success: function (data) {

            if (data.limitAmount.singleLimitAmount == '99999999') {
                $('.panMoney').html('无限')
            } else {
                $('.panMoney').html(threeNumber(data.limitAmount.singleLimitAmount));
            }
            if (data.limitAmount.dayLimitAmount == '99999999') {
                $('.dayMoney').html('无限');
            } else {
                $('.dayMoney').html(threeNumber(data.limitAmount.dayLimitAmount));
            }
            if (data.limitAmount.monthLimitAmount == '99999999') {
                $('.monthMoney').html('无限');

            } else {
                $('.monthMoney').html(threeNumber(data.limitAmount.monthLimitAmount));
            }



        }
    })
    //解绑银行卡
    $('#unbundling').bind('click', function () {
        $('.rebox').show(200);
        $('#cancel1').bind('click', function () {
            $('.rebox').hide();
            $('#payPassword_rsainput').val('');
            $(".guangbiao").css({"left":0});
            $(".sixDigitPassword").find("i").find("b").css({"display":"none"});
        })
        $("#payPassword_rsainput").focus();
        $('#determine1').bind('click', function () {
            $("#determine1").attr("disabled",true);
            setTimeout(function(){
                $("#determine1").attr("disabled",false);
            },300)
            var z = /^[0-9]*$/
            var transactionPassword = $('#payPassword_rsainput').val();
            if (transactionPassword == '') {
                layer.tips('请输入交易密码', '.sixDigitPassword');

            } else if (!z.test(transactionPassword)) {
                layer.tips('交易密码有数字组成', '.sixDigitPassword')
            } else if (transactionPassword.length != 6) {
                layer.tips('交易密码有6位数字组成', '.sixDigitPassword')
            } else {
                //按钮方重复点击
                var loading = layer.load(1, {
                    shade: [0.1,'#fff'] //0.1透明度的白色背景
                });
                $.ajax({
                    type: 'POST',
                    url: '/user/security/cardUnbund',
                    dataType: "json",
                    data: {
                        payPassword: transactionPassword
                    },
                    success: function (data) {
                        if (data.result == 'fail'){
                            layer.close(loading)
                            layer.msg(data.messageText);
                            $('#determine').removeAttr('disabled');
                            $('#payPassword_rsainput').val('').focus();
                            $(".guangbiao").css({"left":0});
                            $(".sixDigitPassword").find("i").find("b").css({"display":"none"});
                        } else {
                            layer.msg('解绑成功');
                            setTimeout(function () {
                                window.location.href = '/user/assets'
                            }, 3000);
                        }
                    }
                })
            }
        })
    })
    //支付弹框
    $("#payPassword_rsainput").bind("blur", function (){
        $(".sixDigitPassword span").removeClass("guangbiao");
    });
    $("#payPassword_rsainput").bind("focus", function (){
        $(".sixDigitPassword span").addClass("guangbiao");
        if($(".i-text").val().length==6){
            $(".guangbiao").css({"left":($(".i-text").val().length-1)*50});
        }else{
            $(".guangbiao").css({"left":($(".i-text").val().length)*50});
        }
    });
    $(".i-text").keyup(function()
    {
        var inp_v = $(this).val();
        var inp_l = inp_v.length;
        //$("p").html( "input的值为：" + inp_v +"; " + "值的长度为:" + inp_l);//测试用

        for( var x = 0; x<=6; x++)
        {
            //$("p").html( inp_l );//测试

            $(".sixDigitPassword").find("i").eq( inp_l ).prevAll("i").find("b").css({"display":"block"});
            $(".sixDigitPassword").find("i").eq( inp_l - 1 ).nextAll("i").find("b").css({"display":"none"});
            $(".guangbiao").css({"left":inp_l * 50});//光标位置
            if( inp_l == 0)
            {
                $(".sixDigitPassword").find("b").css({"display":"none"});
                $(".guangbiao").css({"left":0});
            }
            else if( inp_l == 6)
            {
                $(".sixDigitPassword").find("b").css({"display":"block"});
                $(".guangbiao").css({"left":5 * 50});
            }
        }
    });
    //支付页回车事件
    $(".rebox").keydown(function() {
        if (event.keyCode == "13") {//keyCode=13是回车键
            $('#determine1').click();
        }
    });
});